package org.baicaixiaozhan.learn.guava.collect;

import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;

import java.util.List;

/**
 * DESC: {@link FluentIterable} 示例
 *
 * @author : baicaixiaozhan
 * @since : 2020-11-12
 */
public class FluentIterableDemo {

    public static void main(String[] args) {

        List<Integer> numberList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        // Guava
        FluentIterable.from(numberList)
                .filter(number -> number > 5)
                .limit(3)
                .forEach(System.out::println);

        System.out.println("=====");

        // JDK
        numberList.stream()
                .filter(number -> number > 5)
                .limit(3)
                .forEach(System.out::println);
    }

    public static void useFluent() {
        Multiset<Integer> lengths = HashMultiset.create(
                FluentIterable.from(Lists.newArrayList("AAA", "BBB"))
                        .filter((Predicate<String>) string -> CharMatcher.javaUpperCase().matchesAllOf(string))
                        .transform((Function<String, Integer>) string -> string.length())
        );
    }
    public static void nonUseFluent() {
        Multiset<Integer> lengths = HashMultiset.create();
        for (String string : Lists.newArrayList("AAA", "BBB")) {
            if (CharMatcher.javaUpperCase().matchesAllOf(string)) {
                lengths.add(string.length());
            }
        }
    }
}
